home *** CD-ROM | disk | FTP | other *** search
- /*
- *************************************************************************
- *
- * A Basic Window class
- *
- * A generic simple window that can either show up by itself
- * (and be dragged around on the screen and closed by clicking a
- * "go-away" button)
- * or appear as a special item in a dialog
- *
- * Class ScreenWindow provides a very generic event and control processing
- * that is common to regular windows with controls, modal and modeless
- * dialogs. The basic functionality is to route events to appropriate
- * handlers, process close/destroy/etc events, and handle controls.
- * The class ScreenWindow provides only basic event handling, and it's
- * abstract: you have to create your own class on the base of ScreenWindow
- * and define the draw() function that is called on Update event to
- * paint/repaint the window. You may want to redefine couple of other
- * vanilla handlers (say, for control items (buttons, etc)).
- *
- * When you create the window using explicit specification of the rectangle, title, etc,
- * the window would be invisible right after the creation. It's better to set up
- * invisible (or hidden) attribute in the window resource if you create
- * a window from the resource template. That way you still can do some
- * rearrangements (say, create child windows) before the event processing
- * starts.
- *
- * To show the window (with all the controls, children etc) and start
- * processing of events, call show(). If you need to wait until the window
- * is closed (after the user clicked some button or clicked at the go-away
- * region, or selected "Close" from the menu, or something) call the
- * serve_to_death() function. It returns only when all the opened windows
- * are closed.
- *
- * $Id: Window.h,v 2.2 1994/11/07 20:33:30 oleg Exp oleg $
- *
- ***********************************************************************
- */
-
- #ifndef __GNUC__
- #pragma once
- #else
- #pragma interface
- #endif
-
- #ifndef _Window_h
- #define _Window_h 1
- #include <QDOffscreen.h>
- #include "mymenv.h"
-
- /*
- *----------------------------------------------------------------------
- * Service functions
- */
-
- class IMAGE; // Opaque class
- class Palette;
- typedef Palette ** PaletteHandle;
-
- class ScreenRect : public Rect
- {
- public:
- ScreenRect(const Rect& rect) : Rect(rect) {}
- ScreenRect(const IMAGE& image);
- ScreenRect(const int height, const int width) { left=top=0; right=width; bottom=height; }
- // ScreenRect(const rowcol& heightwidth);
- // Create a rectangle of given height/width
- // positioned at a given point
- // ScreenRect(const rowcol& origin, const rowcol& heightwidth);
- ScreenRect& operator += (const int offset);
- operator Rect * (void) { return this; }
- int q_width(void) const { return right >= left ? right - left : left - right; }
- int q_height(void) const { return bottom >= top ? bottom - top : top - bottom; }
- // Give a STATIC string representation of
- // the rectangle
- // operator const char * (void) const;
- void print(const char * title = "") const;
- };
-
-
- // Make it easy setting and resetting
- // the current GrafPtr
- class SetNewGrafPtr
- {
- GrafPtr old_port;
- public:
- // Switch to an off-screen grafworld
- SetNewGrafPtr(const GrafPtr new_grafport) { GetPort(&old_port); SetPort(new_grafport); }
- // Restore the original grafworld
- ~SetNewGrafPtr(void) { SetPort(old_port); }
- };
-
- /*
- *----------------------------------------------------------------------
- * A generic simple window that can be dragged around the screen
- * and closed by clicking a "go-away" button
- * No other events are handled, redefine the event handler if necessary.
- */
-
- class ScreenWindow
- {
- friend class ModelessDialog;
-
- WindowPtr this_window;
- PaletteHandle private_palette; // Private palette set up by this window, if any
-
- // Boolean Not_canceled; // set to FALSE if Cancel button was pressed
- // or the window was forcibly closed
-
- // static long universal_handler(WINDOW win, EVENT *ep);
- // static int no_opened_windows;
-
- void update(void);
- virtual void draw(void) = 0; // It is this function that really draws smth
-
- // A constructor that does nothing, and initializes
- // nothing. Trick to emulate a virtual constructor
- ScreenWindow(void) : this_window(nil), private_palette(nil) {}
-
- protected:
- WindowPtr our_window(void) const { return this_window; }
-
- // Draw w/o waiting for an UPDATE event:
- // Use for animation
- void draw_immediately(void) { draw(); /*?update()?? */}
-
- virtual void destroy_it(void); // I wouldn't've needed this if a virtual destructor actually
- // worked. Right now, CW 6.0 accepts attribute virtual
- // for a destructor, but doesn't override
- // destructors as it does for virtual functions.
- // Oh, well, another kludge
-
- virtual Boolean handle_mouse_down(const EventRecord& the_event, WindowPtr where_window, short window_part);
- virtual Boolean handle_key_down(const EventRecord& the_event); // Handles key_down & auto_key events
-
- virtual Boolean handle_my_event(const EventRecord& the_event); // Handles my private events...
-
- void set_private_palette(const CTabHandle clut);
-
- public:
- ScreenWindow(ScreenRect rect, const char * title);
- ScreenWindow(const short resource_id); // Create a window from a resource template
- ~ScreenWindow(void) { destroy_it(); }
-
- // This is a post-constructor: unlike a real
- // constructor, it can be virtual, and it _can_
- // call virtual functions. Always call init()
- // after the construction, but _not_ in the
- // constructor itself
- void init(void) { show(); }
- void refresh(void);
- // Serve opened windows until all get closed
- // static void serve_to_death(void);
-
- void show(const Boolean onoff=TRUE) const { ShowHide(this_window, onoff); }
- ScreenRect q_bounds(void) const { return this_window->portRect; }
-
- // virtual void cancel(void); // Close the window
- // operator Boolean (void) const { return Not_canceled; }
- // Boolean is_this_window(const WindowPtr some_window) const { return some_window == this_window; }
- // Boolean operator == (const WindowPtr some_window) const { return is_this_window(some_window); }
-
- // ScreenWindow event dispatch entry points
- // Return FALSE if this object doesn't want any
- // more events
- virtual Boolean handle_null_event(const long event_time);
- virtual Boolean handle_event(const EventRecord& the_event);
- };
-
- /*
- *----------------------------------------------------------------------
- * Color Lookup Table
- */
-
- class CLUTable
- {
- CTabHandle handle;
- bool my_own_handle;
-
- CLUTable(const CLUTable&); // unimplemented: no cloning of
- CLUTable& operator = (const CLUTable&); // CLUTable is allowed
- public:
- CLUTable(void) : handle(nil), my_own_handle(false) {}
- CLUTable(const short clut_id); // Load from a 'clut' resource
- // Wrap around (borrow from) a foreign handle
- CLUTable(const CTabHandle other_handle) : handle(other_handle), my_own_handle(false) {}
-
- ~CLUTable(void) { if( my_own_handle ) DisposeCTable(handle); my_own_handle = false; }
-
- void deep_copy_from(const CLUTable& another_clut); // another_clut may be null!
- bool q_null(void) const { return handle == nil; }
- int q_size(void) const { assert(handle != 0); return (**handle).ctSize; }
- operator CTabHandle (void) const { assert(handle != 0); return handle; }
- void dump(const char title []) const; // Dump the whole contents of the CLUT
- void info(const char title [] = "") const; // Tell briefly about this CLUT
- };
-
- /*
- *----------------------------------------------------------------------
- * An off-screen pixel buffer for faster drawing
- */
-
-
- class OffScreenBuffer
- {
- GWorldPtr graf_world; // (Offscreen) graphical world that contains the picture
-
- OffScreenBuffer(const OffScreenBuffer&); // unimplemented: no copying/cloning of
- OffScreenBuffer& operator = (const OffScreenBuffer&); // OffScreenBuffer is allowed
- protected:
- PixMapHandle pixmap; // Pixmap for the offscreen world
- int _height; // Dimensions of the pixmap (placed here for
- int _width; // easy reference)
- int _bytes_per_row; // Note, bytes_per_row >= width (and generally, not equal)
-
-
- GrafPtr set_this_grafptr(void) const // Set this grafptr and return the old one
- { GrafPtr old_port; GetPort(&old_port); SetPort((GrafPtr)graf_world); return old_port; }
-
- public:
- OffScreenBuffer(ScreenRect rect, const CLUTable& clut=CLUTable());
- ~OffScreenBuffer(void);
-
- // OffScreen world queries...
- PixMapHandle get_pixmap(void) const { return pixmap; }
-
- // Bounding rect of the grafworld
- ScreenRect q_bounds(void) const { return (**pixmap).bounds; }
- int height(void) const { return _height; }
- int width(void) const { return _width; }
- int bytes_per_row(void) const { return _bytes_per_row; }
-
- const CTabHandle q_clut(void) const { return (**pixmap).pmTable; }
-
- // Blit the offscreen buffer on screen
- void draw(const Rect& where_rect, const Rect& from_rect); // only part of it
- void draw(const Rect& where_rect) { draw(where_rect,(**pixmap).bounds); } // or the whole
- void clear(void); // Clear the buffer
-
- // Optimize the OffScreen buffer for a faster
- // drawing on a given color device
- // Return TRUE if the optimization succeeded
- // (bitblitting shall be faster)
- Boolean optimize_color_worlds(const GDHandle g_device);
-
- // Brute imposing of our color world on the
- // g_device: setting up its colortables by force
- // Chances are it would screw colors of all other
- // windows; but if we're the only window on the screen
- // or we don't care...
- // If this imposition succeeds (the function returns
- // TRUE), our grafworld when blitted on screen would
- // certainly look at its (color) best
- Boolean impose_on_gdevice(GDHandle g_device);
- };
-
- // Make it easy setting and resetting
- // the current window
- class Set_NewGrafWorld
- {
- CGrafPtr old_port;
- GDHandle old_gdevice;
- public:
- // Switch to an off-screen grafworld
- Set_NewGrafWorld(GWorldPtr graf_world)
- { GetGWorld(&old_port,&old_gdevice); SetGWorld((CGrafPtr)graf_world,nil); }
- // Restore the original grafworld
- ~Set_NewGrafWorld(void) { SetGWorld(old_port,old_gdevice); }
- };
-
- #endif
-